package com.example.libo.huihui.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.libo.huihui.BR;
import com.example.libo.huihui.R;
import com.example.libo.huihui.activity.ProductDetailActivity;
import com.example.libo.huihui.adapter.BindAdapter;
import com.example.libo.huihui.application.MyApplication;
import com.example.libo.huihui.been.ZixunEntity;
import com.example.libo.huihui.databinding.FragmentZixunBinding;
import java.util.ArrayList;
import java.util.HashMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ZixunFragment extends BaseFragment implements Callback<ZixunEntity>, SwipeRefreshLayout.OnRefreshListener {
    private ArrayList<ZixunEntity.DataBean.ListBean> datas = new ArrayList();
    private BindAdapter adapter;
    private int page = 0;
    private FragmentZixunBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.fragment_zixun,container,false).getRoot();
        binding = DataBindingUtil.bind(view);
        initView();
        return view;
    }

    private void initView(){
        HashMap<Class <?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(ZixunEntity.DataBean.ListBean.class,Pair.create(R.layout.item_zixun, BR.zixun));

        adapter = new BindAdapter(map,datas,getActivity());
        binding.rvZixun.setLayoutManager(new LinearLayoutManager(getActivity()));

        binding.rvZixun.setAdapter(adapter);

        adapter.setCallBack(new BindAdapter.ILoadMore() {
            @Override
            public void loadMore() {
                MyApplication.getHttpService().getZixun(page++).enqueue(ZixunFragment.this);
            }
        });

        binding.srl.setOnRefreshListener(this);

        adapter.setOnItemClickListener(new BindAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                String url = datas.get(position).getHuiZixun().getUrl();
                Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra("url",url);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResponse(Call<ZixunEntity> call, Response<ZixunEntity> response) {
        binding.srl.setRefreshing(false);
        datas.addAll(response.body().getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<ZixunEntity> call, Throwable t) {

    }

    @Override
    public void onRefresh() {
        datas.clear();
        page = 0;
        MyApplication.getHttpService().getZixun(page++).enqueue(this);
    }

    @Override
    protected void lazyLoad() {

    }
}
